<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ServerController
 *
 * @author Administrator
 */
class ServerController extends Controller {

    public function actionIndex() {
        $model = new Server();
        $this->performAjaxValidation($model);
        if (Yii::app()->request->isPostRequest) {
            $model->attributes = Yii::app()->request->getPost('Server');
            if ($model->save()) {
                $this->redirect($this->createUrl('index'));
            }
        }
        $this->model = $model;
        $this->render('index', array('model' => $model));
    }

    public function actionEdit() {
        $model = new Server();
        $id = intval(Yii::app()->request->getParam("id"));
        $this->performAjaxValidation($model);
        $modify = $model->findByPk($id);
        if (Yii::app()->request->isPostRequest) {
            $modify->attributes = Yii::app()->request->getPost('Server');
            if( $modify->save()){
                $this->redirect($this->createUrl('index'));
            }
        }
        $this->render('edit', array('modify' => $modify));
    }

    public function actionDel() {
        $model = new Server();
        $id = intval(Yii::app()->request->getParam("id"));
        $category = $model->findByPk($id);
        if (!$category) {
            $this->render("/layouts/message", array('message' => $model->getAttributeLabel('server_not_exists')));
        } elseif ($category->delete() === false) {
            $this->render("/layouts/message", array('message' => $category->getError('server_delete_failed')));
        } else {
            $this->redirect($this->createUrl('index'));
        }
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'server-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
